package org.lztvn.elearning.front_controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
import org.lztvn.elearning.controllers.AppController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/charts")
public class MyPieChart extends AppController{
	
   @RequestMapping(value="/piechart", method=RequestMethod.GET)
   public void drawPieChart(HttpServletResponse response){
	   response.setContentType("image/png");
	   PieDataset pdSet = createDataSet();
	   JFreeChart chart = createChart(pdSet, "my pie chart");
	   
	   try {
		ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 750, 400);
		response.getOutputStream().close();
	} catch (IOException e) {
		
		e.printStackTrace();
	}
   }
   
   private PieDataset createDataSet(){
	   DefaultPieDataset dpd = new DefaultPieDataset();
	   dpd.setValue("Mac", 23);
	   dpd.setValue("Linux", 35);
	   dpd.setValue("Window", 30);
	   dpd.setValue("Others", 10);
	   return dpd;
   }
   
   private JFreeChart createChart(PieDataset pdSet, String chartTitle){
	   JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, pdSet, true, true, false);
	   PiePlot3D plot =(PiePlot3D)chart.getPlot();
	   plot.setStartAngle(290);
	   plot.setDirection(Rotation.CLOCKWISE);
	   plot.setForegroundAlpha(0.5f);
	 return chart;  
   }
}
